<template>
  <div>
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showaddcateDialogvisible">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格树形功能 -->
      <table-tree :selection-type="false" :data="goodsCatelist" :columns="columns" border :show-row-hover="false"
        :expand-type="false" show-index index-text="#">
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-check" style="color:green" v-if="scope.row.cat_deleted === false"></i>
          <i class="el-icon-close" style="color:red" v-else></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="warning" v-else-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag type="danger" v-else>三级</el-tag>
        </template>
        <template slot="opt" slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="editCate(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="removeCate(scope.row.cat_id)">删除</el-button>
        </template>
      </table-tree>
      <!-- 分页功能 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog title="添加分类" :visible.sync="addcateDialogvisible" width="50%" @close="closeaddDialogvisble">
      <el-form :model="addcateDialogvisibleForm" :rules="addcateDialogvisiblerules" ref="addcateDialogvisibleRef"
        label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addcateDialogvisibleForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级名称">
          <el-cascader v-model="selectedKeys" :options="parentCateList" :props="cascaderProps" @change="parentCateChange"
            clearable change-on-select></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addcateDialogvisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑对话框 -->
    <el-dialog title="编辑" :visible.sync="editCatedialogVisible" width="50%">
      <el-form :model="editDialogruleForm" :rules="editDialogrules" ref="editDialogruleFormRef" label-width="100px">
        <el-form-item label="商品名称" prop="cat_name">
          <el-input v-model="editDialogruleForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCatedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editcateDialog">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Cate',
  data () {
    return {
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      parentCateList: [],
      goodsCatelist: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover'
      },
      // 绑定用户选择的分类值
      selectedKeys: [],
      total: 0,
      addcateDialogvisible: false,
      editCatedialogVisible: false,
      addcateDialogvisibleForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      editDialogruleForm: {
        cat_name: '',
        cat_id: 0

      },
      editDialogrules: {
        cat_name:
          [
            {
              required: true, message: '请输入分类名称', trigger: 'blur'
            }
          ]
      },
      addcateDialogvisiblerules: {
        cat_name: [
          {
            required: true, message: '请输入分类名称', trigger: 'blur'
          }
        ]
      },
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ]
    }
  },
  created () {
    this.getGoodscatelist()
  },
  methods: {
    async getGoodscatelist () {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('请求数据失败')
      this.goodsCatelist = res.data.result
      this.total = res.data.total
    },
    handleSizeChange (newpage) {
      this.queryInfo.pagesize = newpage
      this.getGoodscatelist()
    },
    handleCurrentChange (newnum) {
      this.queryInfo.pagenum = newnum
      this.getGoodscatelist()
    },
    // 获取父级数据列表
    async getcateParent () {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })

      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类列表数据失败')
      }
      this.parentCateList = res.data
    },
    showaddcateDialogvisible () {
      this.addcateDialogvisible = true
      this.getcateParent()
    },
    parentCateChange () {
      if (this.selectedKeys.length > 0) {
        // 则将数组中的最后一项设置为父级分类
        this.addcateDialogvisibleForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        // level也要跟着发生变化
        this.addcateDialogvisibleForm.cat_level = this.selectedKeys.length
      } else {
        this.addcateDialogvisibleForm.cat_pid = 0
        this.addcateDialogvisibleForm.cat_level = 0
      }
    },
    closeaddDialogvisble () {
      this.selectedKeys = []
      this.addcateDialogvisibleForm.cat_name = ''
    },
    addCate () {
      this.$refs.addcateDialogvisibleRef.validate(async valid => {
        if (!valid) return
        // 发送请求完成添加分类
        const { data: res } = await this.$http.post(
          'categories',
          this.addcateDialogvisibleForm
        )

        if (res.meta.status !== 201) {
          return this.$message.error('添加分类失败')
        }

        this.$message.success('添加分类成功')
        this.getGoodscatelist()
        this.addCateDialogVisible = false
      })
    },
    editCate (edit) {
      this.editCatedialogVisible = true
      this.editDialogruleForm.cat_id = edit.cat_id
      this.editDialogruleForm.cat_name = edit.cat_name
    },
    editcateDialog () {
      this.$refs.editDialogruleFormRef.validate(async valid => {
        if (!valid) return this.$message.error('验证失败')
        const { data: res } = await this.$http.put('categories/' + this.editDialogruleForm.cat_id, { cat_name: this.editDialogruleForm.cat_name })
        if (res.meta.status !== 200) return this.$message.error('请求失败')
        this.getGoodscatelist()
        this.$message.success('成功')
        this.editCatedialogVisible = false
      })
    },
    async removeCate (id) {
      const removeconfirm = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (removeconfirm !== 'confirm') return this.$message.info('取消删除')
      const { data: res } = await this.$http.delete('categories/' + id)
      if (res.meta.status !== 200) return this.$message.error('请求失败')
      this.getGoodscatelist()
      this.$message.success('成功')
    }
  }
}

</script>

<style lang="less">
.el-cascader {
  width: 100%;
}

.el-cascader-panel,
.el-cascader-menu {
  height: 300px;
}
</style>
<style scoped lang="less">
/deep/ .el-button {
  margin-bottom: 10px;
}
</style>
